package com.service.impl;

import com.dao.RoleMapper;
import com.domain.*;
import com.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    /*
     * 查询所有角色&&条件查询
     * */
    @Override
    public List<Role> findAllRole(Role role) {
        return roleMapper.findAllRole(role);
    }
    /*
     * 添加角色
     * */
    @Override
    public void saveRole(Role role) {
        Date date = new Date();
        role.setCreatedTime(date);
        role.setUpdatedTime(date);

        role.setCreatedBy("system");
        role.setUpdatedBy("system");

        roleMapper.saveRole(role);
    }
    /*
     * 修改角色
     * */
    @Override
    public void updateRole(Role role) {
        role.setUpdatedTime(new Date());
        role.setUpdatedBy("system");

        roleMapper.updateRole(role);

    }
    /*
     * 根据角色ID查询关联的菜单id
     * */
    @Override
    public List<Integer> findMenuByRoleId(Integer roleId) {
        return roleMapper.findMenuByRoleId(roleId);
    }

    /*
     * 为角色分配菜单信息
     * */
    @Override
    public void roleContextMenu(RoleMenuVO roleMenuVO) {
        roleMapper.deleteRoleContextMenu(roleMenuVO.getRoleId());

        List<Integer> menuIdList = roleMenuVO.getMenuIdList();
        Role_menu_relation relation = new Role_menu_relation();
        for (Integer menuId : menuIdList) {
            relation.setRoleId(roleMenuVO.getRoleId());
            relation.setMenuId(menuId);

            Date date = new Date();
            relation.setCreatedTime(date);
            relation.setUpdatedTime(date);

            relation.setCreatedBy("system");
            relation.setUpdatedby("system");

            roleMapper.roleContextMenu(relation);
        }
    }
    /*
     * 删除角色
     * */
    @Override
    public void deleteRole(Integer id) {
        roleMapper.deleteRoleContextMenu(id);
        roleMapper.deleteRole(id);
    }
    /*
     * 根据角色id查询对应的资源信息
     * */
    @Override
    public List<ResourceCategory> findResourceListByRoleId(Integer id) {
        //获取角色id对应的资源分类列表
        List<ResourceCategory> categories = roleMapper.findResourceCategoryByRoleId(id);
        //遍历列表，在每个资源分类下查找对应角色id的资源
        for (ResourceCategory category : categories) {
            //构架传递参数
            RoleResourceVO roleResourceVO = new RoleResourceVO();
            roleResourceVO.setRoleId(id);
            roleResourceVO.setCategoryId(category.getId());
            //获得对应的资源列表
            List<Resource> resources = roleMapper.findResourceByRoleId(roleResourceVO);
            //将资源列表存入资源分类下的rresourceList
            category.setResourceList(resources);
        }
        return categories;
    }
    /*
     * 为角色分配资源信息
     * */
    @Override
    public void roleContextResource(RoleResourceVO roleResourceVO) {
        //删除角色id对应的在角色资源中间表中的记录
        roleMapper.deleteRoleContextResource(roleResourceVO.getRoleId());
        //获取前台传入的资源列表
        List<Integer> resourceIdList = roleResourceVO.getResourceIdList();
        Role_resource_relation roleResourceRelation = new Role_resource_relation();
        //遍历前台传入的资源列表，逐个添加进中间表
        for (Integer resourceId : resourceIdList) {
            //构建中间表记录
            roleResourceRelation.setRoleId(roleResourceVO.getRoleId());
            roleResourceRelation.setResourceId(resourceId);

            Date date = new Date();
            roleResourceRelation.setCreatedTime(date);
            roleResourceRelation.setUpdatedTime(date);
            roleResourceRelation.setCreatedBy("system");
            roleResourceRelation.setUpdatedby("system");
            //添加记录
            roleMapper.roleContextMResource(roleResourceRelation);
        }
    }
}
